/**
 * rmnet的伙伴角色类
 * @class
 */
class Net_Partner extends Game_Actor {
    initialize(data) {
        this._aptitude = [50, 50, 50, 50, 50, 50];
        super.initialize(2);
        this.setParam(data);
    }

    setParam(data) {
        const { name, exp, classId, characterName, characterIndex, faceName, faceIndex, uid, aptitude } = data;
        this._actorId = 2;
        this._name = name;
        this._characterName = characterName;
        this._characterIndex = characterIndex;
        this._faceName = faceName;
        this._faceIndex = faceIndex;
        this._classId = classId;
        this._exp[this._classId] = 0;
        this._uid = `p-${uid}`;
        this._aptitude = aptitude;
        this.changeExp(exp, false);
        this.recoverAll();
    }

    paramBase(paramId) {
        const lv = this._level;
        const aptitude = this._aptitude;
        let value;
        switch (paramId) {
            case 0:
                value = Math.floor(100 + aptitude[0] / 10 * lv);
                break;
            case 1:
                value = Math.floor(50 + (aptitude[2] / 20 + aptitude[3] / 10) * lv);
                break;
            case 2:
                value = Math.floor(10 + aptitude[1] / 20 * lv);
                break;
            case 3:
                value = Math.floor(10 + aptitude[2] / 20 * lv);
                break;
            case 4:
                value = Math.floor(10 + (aptitude[0] / 10 + aptitude[4] / 10) * lv);
                break;
            case 5:
                value = Math.floor(10 + (aptitude[2] / 20 + aptitude[3] / 10) * lv);
                break;
            case 6:
                value = Math.floor(10 + aptitude[5] / 50 * lv);
                break;
            case 7:
                value = 50;
                break;
            default:
                break;
        }
        return value;
    }
}

/**
 * 伙伴盒，存储玩家所有伙伴的类
 * @class
 */
class Net_PartnerBox {
    constructor() {
        this._data = new Map();
    }

    /**
     * 放入伙伴
     * @param {Net_Partner} partner 伙伴的实例
     */
    putPartner(partner) {
        // const actor = new Net_Partner(data);
        this._data.set(partner._uid, partner);
    }

    partner(uid) {
        return this._data.get(uid);
    }

    allMember() {
        return this._data.values();
    }
}

class Window_PartnerRecruit extends Window_Base {
    initialize(partner) {
        const rect = this.getRect();
        super.initialize(rect);
        this._partner = partner;
        // this.setBackgroundType(3);
        this.createButton();
        this.refresh();
    }

    getRect() {
        const w = 600;
        const h = 365;
        const x = (Graphics.boxWidth - w) / 2;
        const y = (Graphics.boxHeight - h) / 2;
        const rect = new Rectangle(x, y, w, h);
        return rect;
    }

    setPartner(partner) {
        this._partner = partner;
        this.refresh();
    }

    refreshNetBackBitmap() {
        if (this._netBackSprite) {
            const bitmap = this._netBackSprite.bitmap;
            const w = this.width > 0 ? this.width : 0;
            const h = this.height;
            const c = "#2D2D2D";//"#2D2D2D";
            bitmap.resize(w, h);
            bitmap.fillRoundRect(0, 0, w, h, c, 0);
            this._netBackSprite.setFrame(0, 0, w, h);
        }
    }

    createButton() {
        let x = this.width / 2 + 20;
        let y = this.height - 50;
        let text = "招募";
        let bitmap = this.buttonBitmap(text);
        const recruitButton = new Sprite_NetButton(x, y, bitmap, this.recruit.bind(this));
        this.addChild(recruitButton);
        this._recruitButton = recruitButton;
        //
        x = this.width - 120;
        y = this.height - 50;
        text = "离开";
        bitmap = this.buttonBitmap(text);
        const escButton = new Sprite_NetButton(x, y, bitmap, this.esc.bind(this));
        this.addChild(escButton);
        this._escButton = escButton;
    }

    buttonBitmap(text) {
        const bitmap = new Bitmap(100, 60);
        bitmap.fillRoundRect(0, 0, 100, 30, ColorManager.textColor(9));
        bitmap.fillRoundRect(0, 30, 100, 30, ColorManager.textColor(8));
        bitmap.fontSize = 24;
        bitmap.drawText(text, 0, 0, 100, 30, "center");
        bitmap.drawText(text, 0, 30, 100, 30, "center");
        return bitmap;
    }

    recruit() {
        NetManager.parthers.putPartner(this._partner);
        this.hide();
    }

    esc() {
        this._partner = null;
        this.hide();
    }

    refresh() {
        this.contents.clear();
        if (!this._partner) return;
        this.drawTitle();
        this.drawPartnerBase();
        this.drawPartnerParam();
        this.drawPartnerAptitude();
    }

    drawTitle() {
        const text = "伙伴招募";
        this.contents.fontSize = 36;
        this.contents.textColor = "#FFFFFF";
        this.drawText(text, 8, 0, this.width - 16, "center");
    }

    drawPartnerBase() {
        const x = 0;
        const y = 35;
        const w = this.width / 2 - 16;
        const parther = this._partner;
        const characterName = parther.characterName();
        const characterIndex = parther.characterIndex();
        const name = parther.name();
        const level = parther._level;
        this.contents.fontSize = 24;
        this.drawCharacter(characterName, characterIndex, x + 24, y + 48);
        this.drawText(`${name} Lv${level}`, x + 54, y - 5, this.width - 61, "left");
        this.drawText("生命值", x, y + 28, w, "left");
        this.contents.fillRect(x + 80, y + 48, this.width / 2 - 96, 10, "#33FF00");
        this.drawText("魔法值", x, y + 60, w, "left");
        this.contents.fillRect(x + 80, y + 78, this.width / 2 - 96, 10, "#0066FF");
        this.contents.fontSize = 12;
        this.drawText(`${parther.hp}/${parther.mhp}`, x, y + 20, w - 4, "right");
        this.drawText(`${parther.mp}/${parther.mmp}`, x, y + 52, w - 4, "right");
    }

    drawPartnerParam() {
        const x = 0;
        let y = 135;
        const w = this.width / 2 - 16;
        const bc = "#EAEAEA";
        const space = 32
        const partner = this._partner;
        const bitmap = this.contents;
        const config = { "atk": "物攻", "mat": "魔攻", "def": "物防", "mdf": "魔防", "agi": "速度" };
        const list = Object.keys(config);
        bitmap.fontSize = 24;
        for (let i = 0; i < list.length; i++) {
            const name = config[list[i]];
            const value = partner[list[i]];
            if (i % 2 === 0) {
                bitmap.fillRoundRect(x, y, w, 32, bc, 0);
            }
            // this.changeTextColor("#FFFFFF");
            this.drawText(name, x + 8, y, w - 16, "left");
            // this.changeTextColor("#CD6839");
            this.drawText(value, x + 8, y, w - 16, "right");
            y += space
        }
        this.changeTextColor("#FFFFFF");
        this.drawText("天赋", x, y + 10, w, "left");
        let ix = w - 32 - 8;
        for (let i = 0; i < 5; i++) {
            this.drawIcon(16, ix, y + 10);
            ix -= 40;
        }
        // this.resetTextColor();
    }

    drawPartnerAptitude() {
        const x = Math.floor(this.width / 4 * 3);
        const y = Math.floor(this.height / 2 + 20);
        this.contents.fontSize = 30;
        // this.drawText("潜力", x, y, this.width / 2 - 8, "center");
        this.drawAptitude(x, y);
    }

    drawAptitude(x, y) {
        const r = 100;
        const Arr = this.partnerAptitudeArr();
        this.contents.drawRadarChart(450, 160, r, Arr, "#FFFFFF", 1, "#D8BFD8", "#FF0000")
    }

    partnerAptitudeArr() {
        const parther = this._partner;
        const aptitude = parther._aptitude;
        const atk = Math.min(aptitude[1], 100);
        const mat = Math.min(aptitude[3], 100);
        const def = Math.min(aptitude[2], 100);
        const mdf = Math.min(aptitude[4], 100);
        const mhp = Math.min(aptitude[0], 100);
        const agi = Math.min(aptitude[5], 100);
        const Arr = [
            {
                "cur": mhp,
                "name": "体质",
                "max": 110
            },
            {
                "cur": mat,
                "name": "智力",
                "max": 110
            },
            {
                "cur": mdf,
                "name": "精神",
                "max": 110
            },
            {
                "cur": agi,
                "name": "敏捷",
                "max": 110
            },
            {
                "cur": def,
                "name": "意志",
                "max": 110
            },
            {
                "cur": atk,
                "name": "力量",
                "max": 110
            }
        ];
        return Arr;
    }
}

sun_rmnet.setSubject = Game_Action.prototype.setSubject
Game_Action.prototype.setSubject = function (subject) {
    this._subjectActorUid = subject.uid;
    sun_rmnet.setSubject.call(this, subject);
}
sun_rmnet.subject = Game_Action.prototype.subject;
Game_Action.prototype.subject = function () {
    const symbol = this._subjectActorUid;
    if (symbol) return NetManager.partners.actor(symbol);
    return sun_rmnet.subject.call(this);
}

function ceshi() {
    const parther = makerPartner();
    const window = SceneManager._scene.ceshi;
    if (window) {
        window.setPartner(parther);
        window.show();
        return;
    }
    const _window = new Window_PartnerRecruit(parther);
    SceneManager._scene.addWindow(_window);
    SceneManager._scene.ceshi = _window;
}

function makerPartner() {

    const uid = Math.randomInt(10000);
    const aptitude = [];
    for (i = 0; i < 6; i++) {
        aptitude.push(50 + Math.randomInt(50));
    }
    const data = {
        "name": "陈安",
        "exp": 100000,
        "classId": 1,
        "characterName": "class",
        "characterIndex": 2,
        "faceName": "class",
        "faceIndex": 2,
        "uid": uid,
        "aptitude": aptitude
    }
    return new Net_Partner(data);
}

Window_StatusBase.prototype.placeGauge = function (actor, type, x, y) {
    const key = "actor%1-gauge-%2".format(actor._uid, type);
    const sprite = this.createInnerSprite(key, Sprite_Gauge);
    sprite.setup(actor, type);
    sprite.move(x, y);
    sprite.show();
}

NetManager.parthers = new Net_PartnerBox();